/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"github.com/jinzhu/gorm"
	"gitee.com/tugoer/psy-testing/code/model"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

var Subject = []*mux.Action{
	{
		Name: "Get Subject",
		Method: "GET",
		Route: "/subject",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&getSubject{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getSubject{}
			ctx.WarnParse(p)
			sub, err := model.GetSubject(p.OriginalID)
			if err != nil {
				if err == gorm.ErrRecordNotFound {
					ctx.MsgWarning("该测试已被删除", "")
				} else {
					ctx.ErrWarning(err)
				}
			} else {
				ctx.SendJson(sub)
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Subjects},

			Summary: "获取主题内容",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: model.Subject{},
				},
			},
		},
	},
	{
		Name: "Get Subjects",
		Method: "GET",
		Route: "/subjects",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&getSubjects{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getSubjects{}
			ctx.WarnParse(p)
			var ss []*model.Subject
			var err error
			switch p.Kind {
			case "latest":
				ss, err = model.SelectLatestSubs(p.Limit, p.Offset)
			case "hot":
				ss, err = model.SelectHottestSubs(p.Limit, p.Offset)
			case "recommend":
				ss, err = model.SelectRecommendSubs(p.Limit, p.Offset)
			}
			ctx.ErrWarning(err)
			ctx.SendJson(ss)
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Subjects},
			Summary: "按类型获取主题列表",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: []model.Subject{},
				},
			},
		},
	},
	{
		Name: "Search Subjects",
		Method: "GET",
		Route: "/search-subjects",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&searchSubjects{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &searchSubjects{}
			ctx.WarnParse(p)
			ss, err := model.SearchSubjects(p.Title)
			ctx.ErrWarning(err)
			ctx.SendJson(ss)
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Subjects},
			Summary: "按标题搜索主题",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: []model.Subject{},
				},
			},
		},
	},
	{
		Name: "Create Subject",
		Method: "POST",
		Route: "subject",
		Params: mux.Parameters{
			{
				In: mux.Form,
				Schema: &model.Subject{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Subject{}
			ctx.WarnParse(p)
			ctx.ErrWarning(p.Create())
			ctx.SendJson(map[string]int{
				"id": p.ID,
			})
		},
		ApiDoc: &mux.ApiDoc{
			Tags:[]string{tags.Subjects},
			Summary: "创建主题",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example:map[string]int{"id": 2314},
				},
			},
		},
	},
	{
		Name: "Update Subject",
		Method: "PUT",
		Route: "subject",
		Params: mux.Parameters{
			{
				In: mux.Form,
				Schema: &model.Subject{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Subject{}
			ctx.WarnParse(p)
			ctx.ErrWarning(p.Update())
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc: &mux.ApiDoc{
			Tags:[]string{tags.Subjects},
			Summary: "修改主题",
		},
	},
	{
		Name: "Delete Subject",
		Method: "DELETE",
		Route: "subject",
		Params: mux.Parameters{
			{
				In: mux.Query,
				Schema: &delSubject{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &delSubject{}
			ctx.WarnParse(p)
			ctx.ErrWarning(model.DeleteSubject(p.OriginalID))
			ctx.MsgSuccess("删除成功", "")
		},
		ApiDoc: &mux.ApiDoc{
			Tags:[]string{tags.Subjects},
			Summary: "删除主题",
		},
	},
	//{
	//	Name: "Get Subject Types",
	//	Method: "GET",
	//	Route: "/subject-types",
	//	Handler: func(ctx *mux.Context) {
	//		ctx.SendJson(code.Types)
	//	},
	//	ApiDoc:&mux.ApiDoc{
	//		Tags:[]string{tags.Subjects},
	//		Summary: "获取所有主题的类型",
	//		Responses:orivil.Responses{
	//			"200": {
	//				Description:"success",
	//				Example: code.Types,
	//			},
	//		},
	//	},
	//},
}

type delSubject getSubject

type getSubject struct {
	OriginalID string `required:"ID 不能为空"`
}

type getSubjects struct {
	Kind string `required:"查询类型不能为空" enum:"latest hot recommend" enum-msg:"只能为latest/hot/recommend" desc:"latest/hot/recommend"`
	Limit int `required:"limit 不能为空"`
	Offset int `required:"offset 不能为空"`
}

type searchSubjects struct {
	Title string `desc:"搜索标题" required:"Title 不能为空"`
}